package com.liangzhong.modules.friend.mediator
{
	import com.common.queueloader.QueueLoader;
	import com.common.queueloader.QueueLoaderEvent;
	import com.common.queueloader.QueueVo;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.constants.ResourceConst;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.FriendEvent;
	import com.liangzhong.events.ModuleEvent;
	import com.liangzhong.manager.ResourceManager;
	import com.liangzhong.mediator.ModuleMediator;
	import com.liangzhong.model.AppData;
	import com.liangzhong.proxy.friend.FriendProxy;
	import com.liangzhong.proxyconstant.friend.FriendProxyConstant;
	import com.lz.football.module.base.IModuleView;
	import com.lz.football.module.friend.IFriendView;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	
	import org.puremvc.as3.interfaces.INotification;
	
	/**
	 *好友 
	 * @author Administrator
	 * 
	 */	
	public class FriendMediator extends ModuleMediator
	{
		public static const MediatorName :String = "FriendMediator";
		public function FriendMediator(mediatorName:String=null, viewComponent:Object=null, param:Object=null)
		{
			super(mediatorName, viewComponent, param);
		}
		/**
		 *获取 实例 
		 * @return 
		 * 
		 */		
		public function get view():IFriendView
		{
			return viewComponent as IFriendView;
		}
		/**
		 *添加事件 
		 * 
		 */		
		override public function addEventListeners():void
		{
			(view as DisplayObject).addEventListener(ModuleEvent.REMOVE_MODULE,onRemoveModuleHandler);
			AppEventDispatcher.getInstance().addEventListener(FriendEvent.AGGREE_INVITE,onAgreeHandler);
			AppEventDispatcher.getInstance().addEventListener(FriendEvent.REFUSE_INVITE,onrefuseHandler);
			AppEventDispatcher.getInstance().addEventListener(FriendEvent.INVITE_FRIENDS,onINVITE_FRIENDSHandler);
			
			AppEventDispatcher.getInstance().addEventListener(FriendEvent.NAME_INVITE,onNameInviteHandler);
			AppEventDispatcher.getInstance().addEventListener(FriendEvent.RECOMMEND_LIST,onRecommendList);
			
			AppEventDispatcher.getInstance().addEventListener(FriendEvent.OPEN_RECEIVE_PANEL,oniveiteList);
			AppEventDispatcher.getInstance().addEventListener(FriendEvent.DELETE_FRIEND,onDeleteFriend);
		}
		
		/**
		 *删除好友 
		 * @param event
		 * 
		 */
		protected function onDeleteFriend(event:FriendEvent):void
		{
			// TODO Auto-generated method stub
			this.sendNotification(FriendProxyConstant.FRIEND_MEDIATOR_PROXY,event.param,FriendProxyConstant.DELETE_FRIEND);
		}
		
		/**
		 * 受邀请列表
		 * @param event
		 * 
		 */
		protected function oniveiteList(event:FriendEvent):void
		{
			// TODO Auto-generated method stub
			this.sendNotification(FriendProxyConstant.FRIEND_MEDIATOR_PROXY,event.param,FriendProxyConstant.RECEIVE_INVITE);
		}
		
		protected function onRecommendList(event:FriendEvent):void
		{
			// TODO Auto-generated method stub
			this.sendNotification(FriendProxyConstant.FRIEND_MEDIATOR_PROXY,event.param,FriendProxyConstant.RECOMMEND_FRIEND);
		}		
		
		
		/**
		 *详细名称邀请 
		 * @param event
		 * 
		 */
		protected function onNameInviteHandler(event:FriendEvent):void
		{
			// TODO Auto-generated method stub
			this.sendNotification(FriendProxyConstant.FRIEND_MEDIATOR_PROXY,event.param,FriendProxyConstant.NAME_INVITE);
		}
		
		/**
		 *查看好友  
		 * @param event
		 * 
		 */
		//		protected function onLookUpFirend(event:FriendEvent):void
		//		{
		//			this.sendNotification(FriendProxyConstant.FRIEND_MEDIATOR_PROXY,event.param,FriendProxyConstant.NAME_INVITE);
		//		}
		
		/**
		 * 邀请为好友 
		 * @param event
		 * 
		 */
		protected function onINVITE_FRIENDSHandler(event:FriendEvent):void
		{
			// TODO Auto-generated method stub
			this.sendNotification(FriendProxyConstant.FRIEND_MEDIATOR_PROXY,event.param,FriendProxyConstant.INVITE_BY_ID);
		}
		
		/**
		 * 同意好友的邀请 
		 * @param event
		 * 
		 */
		protected function onAgreeHandler(event:FriendEvent):void
		{
			// TODO Auto-generated method stub
			this.sendNotification(FriendProxyConstant.FRIEND_MEDIATOR_PROXY,event.param,FriendProxyConstant.AGREE_FRIEND_INVITE);
		}
		/**
		 * 拒绝好友的邀请 
		 * @param event
		 * 
		 */
		protected function onrefuseHandler(event:FriendEvent):void
		{
			// TODO Auto-generated method stub
			this.sendNotification(FriendProxyConstant.FRIEND_MEDIATOR_PROXY,event.param,FriendProxyConstant.REFUSE_FRIEND_INVITE);
		}
		protected function onRemoveModuleHandler(event:ModuleEvent):void
		{
			removeMediator();
		}
		/**
		 * 删除事件 
		 * 
		 */
		override public function removeEventListeners():void
		{
			(view as DisplayObject).removeEventListener(ModuleEvent.REMOVE_MODULE,onRemoveModuleHandler);
			AppEventDispatcher.getInstance().removeEventListener(FriendEvent.AGGREE_INVITE,onAgreeHandler);
			AppEventDispatcher.getInstance().removeEventListener(FriendEvent.REFUSE_INVITE,onrefuseHandler);
			AppEventDispatcher.getInstance().removeEventListener(FriendEvent.INVITE_FRIENDS,onINVITE_FRIENDSHandler);
			
			AppEventDispatcher.getInstance().removeEventListener(FriendEvent.NAME_INVITE,onNameInviteHandler);
			AppEventDispatcher.getInstance().removeEventListener(FriendEvent.RECOMMEND_LIST,onRecommendList);
			
			AppEventDispatcher.getInstance().removeEventListener(FriendEvent.OPEN_RECEIVE_PANEL,oniveiteList);
			AppEventDispatcher.getInstance().removeEventListener(FriendEvent.DELETE_FRIEND,onDeleteFriend);
		}
		/**
		 * 监听信息
		 * @return 
		 * 
		 */		
		override public function listNotificationInterests():Array
		{
			return [FriendProxyConstant.GET_NAME_INVITE,
				FriendProxyConstant.UPDATA_FRIEND_LIST,
				FriendProxyConstant.UPDATE_RECOMMEND_FRIEND_LIST,
				FriendProxyConstant.GET_RECEIVE_INVITE].concat(super.listNotificationInterests());
		}
		/**
		 * 处理信息
		 * @param notification
		 * 
		 */		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case FriendProxyConstant.GET_NAME_INVITE://名称邀请成功
					view.nameInvite(notification.getBody());
					break;
				case FriendProxyConstant.UPDATA_FRIEND_LIST://更新好友的邀请列表 
					//					if(view == null)
					//					{
					//						showUI();
					//					}
					if(view != null)
					{
						view.updataFriendList(notification.getBody());
					}
					break;
				case FriendProxyConstant.UPDATE_RECOMMEND_FRIEND_LIST://邀请好友
					view.updataInviteList(notification.getBody());
					break;
				case FriendProxyConstant.GET_RECEIVE_INVITE://受到邀请的列表
					view.getreceive_invite(notification.getBody());
					break;
				
			}
		}
		/**
		 * 
		 * 注册
		 */		
		override public function onRegister():void
		{
			//			if(AppData.getInstance().friendManager.friendList== null)
			//			{
			
			//			}
			resName = ResourceConst.FRIEND_SWF;
			if(ResourceManager.instance.isContainResource(ResourceConst.FRIEND_SWF))
			{
				showUI();
			}
			else
			{
				loader = new QueueLoader();
				var clubConfig :XML = XML(ResourceManager.instance.getResource(ResourceConst.MODULE_RES_CONFIG)).friend[0];
				bytesTotal = int(clubConfig.@totalBytes);
				var releaseVersion :String;
				for each(var item :XML in clubConfig.item)
				{
					releaseVersion = "?" + item.@version + "." + item.@itemType;
					loader.addQueue(new QueueVo(item.@resName,item.@id,item.@id),item.@url + releaseVersion);
				}
				loadRes();
			}
		}
		/**
		 *加载资源完毕 
		 * @param e
		 * 
		 */		
		override protected function onLoadComplete(e:QueueLoaderEvent):void
		{
			
			//			showUI();
			super.onLoadComplete(e);
			ResourceManager.instance.putResource(ResourceConst.FRIEND_SWF,resList);
			//			if(AppData.getInstance().friendManager.friendList!= null)
			//			{
			showUI();
			//			}
		}
		/**
		 *显示 
		 * 
		 */		
		private function showUI():void
		{
			if(AppData.getInstance().time - AppData.getInstance().friendManager.checkTime > 1800)
			{
				this.sendNotification(FriendProxyConstant.FRIEND_MEDIATOR_PROXY,null,FriendProxyConstant.FRIEND_INFO);
			}
			
			
			var cView :IModuleView = Reflection.createInstance("com.lz.football.module.friend.FriendView");
			cView.initUI();
			viewComponent = cView;
			//			if(AppData.getInstance().friendManager.friendList!= null)
			//			{
			//				view.updataFriendList({type:1});
			//			}
			GlobalDef.GameWindowContainer.addChild(viewComponent as DisplayObject);
			addEventListeners();
			if(AppData.getInstance().friendManager.friendList!= null)
			{
				view.updataFriendList({type: 1});
			}
		}
		/**
		 * 删除 mediator
		 * 
		 */		
		override public function removeMediator():void
		{
			facade.removeMediator(MediatorName);
		}
		/**
		 *删除 清理 
		 * 
		 */		
		override public function onRemove():void
		{
			super.onRemove();
			removeEventListeners();
			(viewComponent as IModuleView).destroyView();
			viewComponent = null;
		}
	}
}